package edu.uta.cse5322_2011_team3;

import java.util.ArrayList;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import edu.stanford.nlp.ling.TaggedWord;
import edu.stanford.nlp.parser.lexparser.LexicalizedParser;
import edu.stanford.nlp.trees.Tree;

public class NLPParser {
	private static NLPParser instance = null;
	private LexicalizedParser lexicalizedParser = null;

	public static NLPParser getInstance() {
		if (instance == null) {
			instance = new  NLPParser();
		}
		return instance;
	}

	private NLPParser() {
		lexicalizedParser = new LexicalizedParser(Config.dictionaryPath);
		lexicalizedParser.setOptionFlags("-outputFormat", "wordsAndTags");
	}

	public String parse(String sentence) {
		Tree parse = lexicalizedParser.apply(sentence);
		ArrayList<TaggedWord> taggedWords = parse.taggedYield();

		StringBuilder taggedSentence = new StringBuilder();
		for (TaggedWord taggedWord : taggedWords) {
			taggedSentence.append(taggedWord.toString() + " ");

		}
		return taggedSentence.toString();

	}
	
	public static String removeTags(String taggedWord) {
		Pattern cleanUpPattern = Pattern.compile("/[A-Z]+");
        Matcher cleanUp = cleanUpPattern.matcher(taggedWord);
        return cleanUp.replaceAll("").trim();
	}
	
	public static String normalizeRequirement(String sentence) {
		Pattern cleanUpPattern = Pattern.compile("R\\d+(\\.\\d+)*");
        Matcher cleanUp = cleanUpPattern.matcher(sentence.trim());
        return cleanUp.replaceAll("").trim();
	}
}
